﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Autodesk.Revit.DB;

namespace SectionSignAndFilled.MaterialManager
{
	internal class MateriaData
	{
		public MateriaData(string materialName, Document doc)
		{
			this.Name = materialName;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			List<Element> list = (from e in filteredElementCollector.OfClass(typeof(Material))
			where e.Name == this.Name
			select e).ToList<Element>();
			FillPattern fillPattern = null;
			if (list.Count != 0)
			{
				Material material = list[0] as Material;
				Element element = material.Document.GetElement(material.CutPatternId);
				if (element != null)
				{
					FillPatternElement fillPatternElement = element as FillPatternElement;
					fillPattern = fillPatternElement.GetFillPattern();
				}
			}
			OneScale oneScale = new OneScale();
			oneScale.Scale = 1.5;
			if (fillPattern != null)
			{
				oneScale.NameModify = fillPattern.Name;
			}
			oneScale.document = doc;
			OneScale oneScale2 = new OneScale();
			oneScale2.Scale = 0.5;
			if (fillPattern != null)
			{
				oneScale2.NameModify = fillPattern.Name;
			}
			oneScale2.document = doc;
			this.ScalesData.Add(oneScale);
			this.ScalesData.Add(oneScale2);
		}

		public string GetMateriaByMap()
		{
			return "";
		}

		public bool WriteData(XmlDocument xmlDoc, XmlNode root, XmlNodeList childNodes)
		{
			XmlNode xmlNode = XMLTools.FindNodeByAtt(childNodes, "Name", this.Name);
			if (xmlNode == null)
			{
				xmlNode = xmlDoc.CreateElement("MateriaData");
				(xmlNode as XmlElement).SetAttribute("Name", this.Name);
				xmlNode = root.AppendChild(xmlNode);
			}
			XmlNodeList childNodes2 = xmlNode.ChildNodes;
			for (int i = 0; i < this.ScalesData.Count; i++)
			{
				this.ScalesData[i].WriteData(xmlDoc, xmlNode, childNodes2);
			}
			return true;
		}

		public bool ReadData(XmlNodeList childNodes)
		{
			XmlElement xmlElement = null;
			for (int i = 0; i < childNodes.Count; i++)
			{
				XmlElement xmlElement2 = childNodes[i] as XmlElement;
				string attribute = xmlElement2.GetAttribute("Name");
				if (attribute == this.Name)
				{
					xmlElement = xmlElement2;
					break;
				}
			}
			if (xmlElement == null)
			{
				return false;
			}
			XmlNodeList childNodes2 = xmlElement.ChildNodes;
			for (int j = 0; j < this.ScalesData.Count; j++)
			{
				this.ScalesData[j].ReadData(childNodes2);
			}
			return true;
		}

		public Document document;

		public string Name = "";

		public List<OneScale> ScalesData = new List<OneScale>();
	}
}
